﻿@{
    ViewBag.Title = "机构管理";
    Layout = "~/Views/Shared/_EasyUILayout.cshtml";
}

<script type="text/javascript">
    $(function () {
        loadData();
    });

    //加载数据
    function loadData() {
        $('#dg').treegrid({
            url: '@Url.Action("DataDemand")',
            fit: true,//自动填充
            idField: 'id',
            treeField: 'Name',
            columns: [[

                { field: 'Name', title: '名称', width: 400, halign: 'center', align: 'left', sortable: false },
                { field: 'AttributeName', title: '属性', width: 150, halign: 'center', align: 'center', sortable: false }
            ]],
            rowStyler: function (row) {
                if (row && row.State == 0) {
                    return 'color:#ff0000';
                }
            }
        });
    }

    //工具栏按钮单击事件
    function toolbarClick(key) {
        switch (key) {
            case "Add":
                addInfo();
                break;
            case "Edit":
                editInfo();
                break;
            case "Delete":
                deleteInfo();
                break;
            case "Enable":
                enableInfo();
                break;
            case "Disable":
                disableInfo();
                break;
        }
    }

    //添加信息
    function addInfo() {
        var row = $('#dg').treegrid('getSelected');
        var parentId = row == null ? '' : row.id;
        common.showDialog('@Url.Action("Edit")?parentId=' + parentId, '添加机构', 550, 400, function (result) {
            $('#dg').treegrid('reload');
        });
    }

    //修改信息
    function editInfo() {
        var row = $('#dg').treegrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        common.showDialog('@Url.Action("Edit")?id=' + row.id, '编辑机构', 550, 400, function (result) {
            common.processResult(result, function () {
                $('#dg').treegrid('reload');
            });
        });
    }

    //删除信息
    function deleteInfo() {
        var row = $('#dg').treegrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        common.confirm('你确认要删除[<span class="msg_tip">' + row.text + '</span>]吗?', function () {
            var postData = new Object();
            postData.id = row.id;
            common.antiToken(postData);

            $.post('@Url.Action("Delete")', postData, function (result) {
                common.processResult(result, function () {
                    $('#dg').treegrid('remove', row.id);
                });
            });
        });
    }

    //启用信息
    function enableInfo() {
        var row = $('#dg').treegrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        common.confirm('你确认要启用[<span class="msg_tip">' + row.text + '</span>]吗?', function () {
            var postData = new Object();
            postData.id = row.id;
            common.antiToken(postData);

            $.post('@Url.Action("Enable")', postData, function (result) {
                common.processResult(result, function () {
                    row.State = 1;
                    updateNode(row.ParentId, row.id, row);
                });
            });
        });
    }

    //禁用信息
    function disableInfo() {
        var row = $('#dg').treegrid('getSelected');
        if (row == null) {
            common.alert('请先选择一条记录。');
            return;
        }

        common.confirm('你确认要禁用[<span class="msg_tip">' + row.text + '</span>]吗?', function () {
            var postData = new Object();
            postData.id = row.id;
            common.antiToken(postData);

            $.post('@Url.Action("Disable")', postData, function (result) {
                common.processResult(result, function () {
                    row.State = 0;
                    updateNode(row.ParentId, row.id, row);
                });
            });
        });
    }

    //查询
    function query() {
        var keyword = $('#txtKeyword').textbox('getValue');

        //为空时恢复原来的状态
        if (keyword == '') {
            $('#dg').treegrid({
                queryParams: { targetId: '' },
                onLoadSuccess: function () { }
            });

            $('#win').window('close');
            return;
        }

        //先找出匹配的模块项
        $.getJSON('@Url.Action("Search")?keyword=' + encodeURI(keyword), function (result) {
            if (result.length > 1) {
                //两项以上，在左边显示导航表格
                loadSearchResult(result);

                $('#win').window('open')
            }
            else {
                $('#win').window('close')

                //只有一个匹配的，直接跳转
                if (result.length == 1) {
                    gotoNode(result[0].OrgID);
                }
                else {
                    common.popup('没有查询到内容。');
                }
            }
        });
    }

    //在左边显示查询到的条目
    function loadSearchResult(result) {
        $('#dgNavigate').datagrid({
            fit: true,
            singleSelect: true,
            border: false,
            columns: [[
                { field: 'FullName', title: '匹配的机构', width: 400 }
            ]],
            onClickRow: function (index, row) {
                gotoNode(row.OrgID);
            }
        });

        $('#dgNavigate').datagrid('loadData', result);
    }

    //跳转到指定的模块并选中
    function gotoNode(id) {
        $('#dg').treegrid({
            queryParams: { targetId: id },
            onLoadSuccess: function () {
                var node = $('#dg').treegrid('select', id);
            }
        });
    }


    //添加新节点
    function appendNode(r) {
        if (r.ParentId) {
            var parent = $('#dg').treegrid('find', r.ParentId);
            if (parent == null) {
                gotoNode(r.id);
                return;
            }
        }

        $('#dg').treegrid('append', {
            parent: r.ParentId == '' || r.ParentId == null ? null : r.ParentId,
            data: [{
                id: r.id,
                Name: r.Name,
                State: r.State
            }]
        });

        $('#dg').treegrid('select', r.id);
    }

    //更新节点信息
    function updateNode(pid, id, r) {
        //如果父节点没有改变
        if (pid == r.ParentId) {
            $('#dg').treegrid('update', {
                id: id,
                row: {
                    Name: r.Name,
                    State: r.State
                }
            });
        }
        else {
            $('#dg').treegrid('remove', r.id);
            appendNode(r);
        }
    }
</script>
@Html.AntiForgeryToken()
<div data-options="region:'center',border:false">
    <div class="easyui-layout" data-options="fit:true" style="width: 100%; height: 100%">
        <div data-options="region:'north',border:false">
            <div class="datagrid-toolbar">
                @Html.Toolbar()
            </div>
            <div class="query">
                <table>
                    <tr>
                        <td>快速查询</td>
                        <td>
                            <input id="txtKeyword" type="text" class="easyui-textbox enterQuery" data-options="prompt:'编码、名称或全称'" style="width: 220px" />
                        </td>
                        <td>
                            <a class="easyui-linkbutton" id="btnQuery" onclick="query()" data-options="iconCls:'icon-search'">查询</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>
        <div data-options="region:'center',border:false">
            <table id="dg"></table>
        </div>
    </div>
</div>
<div id="win" class="easyui-window" title="查找结果" data-options="collapsible:false,minimizable:false,maximizable:false,closable:true,closed:true" style="width: 450px; height: 400px;">
    <table id="dgNavigate"></table>
</div>